package com.mbbmap.app.home;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mbbmap.app.manager.ConfigManager;
import com.mbbmap.app.manager.DBConnectionManager;
import com.mbbmap.app.manager.SystemLogManager;
import com.mbbmap.util.Constants;
import com.mbbmap.util.EnhancedException;

public class SecParameterHome {
	 private static SecParameterHome instance = null;
	 
    /**
     * Returns the single instance, creating one if it's the
     * first time this method is called.
     *
     * @return IslamicContractHome. The single instance.
     */
	 public static SecParameterHome getInstance() {
	         //thread-safe implementation
	         if (instance == null) {
	                 synchronized (SecParameterHome.class) {
	                         if (instance == null) {
	                                 instance = new SecParameterHome();
	                         }
	                 }
	         }
	         return instance;
	 }

	 private SecParameterHome() {
	         super();
	 }
		 
		 
	/**
	 * @author Luiz Santos
	 * @date 12-20-2013
	 * @desc Updates Regex Validation
	*/
     public String updParameter(String param_name, String param_value, String strLoginID, String strMerchant) {

             String strReturn = "N";

             int TIMEOUT = 10000; //DB connection timeout if unable to get connection in less than 10 seconds
             DBConnectionManager connMgr;
             String connectionPool;
             SystemLogManager sysLogMgr;

             connectionPool = ConfigManager.getInstance().getConnectionPool();
             connMgr = DBConnectionManager.getInstance();
             String timeOutValue = ConfigManager.getInstance().get(Constants.DB_WAIT_TIME);
             if (timeOutValue != null) {
                     try {
                             TIMEOUT = Integer.parseInt(timeOutValue);
                     } catch (NumberFormatException e) {}
             }
             sysLogMgr = SystemLogManager.getInstance();

             Connection conn = connMgr.getConnection(connectionPool, TIMEOUT);

             PreparedStatement stmt = null;
             ResultSet rs= null;

             try {
                     if (conn == null) {
                             EnhancedException ex = new EnhancedException("");
                             ex.setExceptionLocation("SecParameterHome_updPwdRegex_001 [Connection at : " + connectionPool + "]");
                             ex.setExtraMessage("Unable to get connection. See DBConnectionManager.log");
                             sysLogMgr.logError(ex);
                     } //if (conn == null)
                     else{
                             StringBuffer sqlStmt = new StringBuffer("");
                             if (param_value != null && param_name != null && strLoginID != null && strMerchant != null){
                                     
                            	 	java.util.Date creationDate = new java.util.Date();
                            	 	
                            	 	sqlStmt.append("UPDATE TBLPARAMETERS SET PARAM_VALUE ='" + param_value + "' WHERE PARAM_NAME = '" + param_name + "'");
                                    //sqlStmt.append("INSERT INTO TBLACCESSROLE (MER_NAME,ROLE_CODE,ROLE_NAME,CREATED_BY,CREATED_DATE) VALUES ('" + strMerchant + "','" + strGroupCode + "','" + strGroupName + "','" + strLoginID + "', ? )");
                                    System.out.println("SecParameterHome_updPwdRegex_001 sqlStmt.toString()" + sqlStmt.toString());
                                    PreparedStatement updatePstmt = null;

                                    updatePstmt = conn.prepareStatement(sqlStmt.toString());
                                    //updatePstmt.setTimestamp(1, new java.sql.Timestamp(creationDate.getTime()));
                                    updatePstmt.executeUpdate();
                                    updatePstmt.close();
                                    strReturn = "Y";
                                     //System.out.println("BillActHome Resultset . updatePayHistory()");
                             }else{
                                     strReturn = "N";
                             } // End if all parameters not null
                     } //[ELSE] if (conn == null)
             } catch (Exception e) {
                     EnhancedException ex = new EnhancedException(e.toString());
                     ex.setExceptionLocation("SecParameterHome_updPwdRegex_002 [Connection at : " + connectionPool + "]");
                     ex.setExtraMessage("Exception Encountered");
                     sysLogMgr.logError(ex);
             } catch (Throwable t) {
                     EnhancedException ex = new EnhancedException(t.toString());
                     ex.setExceptionLocation("SecParameterHome_updPwdRegex_003 [Connection at : " + connectionPool + "]");
                     ex.setExtraMessage("Throwable Encountered");
                     sysLogMgr.logError(ex);
             } finally {
                     if (stmt != null) {
                             try {
                                     stmt.close();
                             } catch (SQLException e1) {}
                     }
                     connMgr.freeConnection(connectionPool, conn);
             }
             return strReturn;
     }	 
		 
		 
		 
		 
		 
		 
		 
		 
	 
}
